package com.example.teamtribe.main.club.setup;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.PhotoActivity;
import com.example.teamtribe.common.PopupPics;
import com.jph.takephoto.model.TResult;

import butterknife.BindView;

public class SetUpActivity extends PhotoActivity implements View.OnClickListener, SetUpContract.View, PopupPics.OnPickListener {

    @BindView(R.id.iv_head) ImageView ivHead;
    @BindView(R.id.et_name) EditText etName;
    @BindView(R.id.et_type) EditText etType;
    @BindView(R.id.et_comment) EditText etComment;
    private SetUpContract.Presenter presenter;
    private PopupPics popupPics;
    private String head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_up);
        ((TextView) findViewById(R.id.tv_title)).setText("创建社团");
        ivHead.setOnClickListener(this);
        findViewById(R.id.fab_yes).setOnClickListener(this);
        presenter = new SetUpPresenter(this);
        popupPics = new PopupPics(this, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_head:
                popupPics.show(v);
                break;
            case R.id.fab_yes:
                presenter.setUp(head, etName.getText().toString().trim(), etType.getText().toString().trim(), etComment.getText().toString().trim());
                break;
            default:
                break;
        }
    }

    @Override
    public void next() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(getUri(), getCropOptions());
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromCaptureWithCrop(getUri(), getCropOptions());
    }

    @Override
    public void takeSuccess(TResult result) {
        head = result.getImage().getOriginalPath();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(SetUpActivity.this).load(head).into(ivHead);
            }
        });
    }
}
